package com.hgvip.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.List;

/**
 * Created by arnold.zhu on 2017/9/20.
 */
public class KaoLaUtil {

    /***
     * 根据参数发起请求
     *
     * @param url
     * @param params
     * @return
     */
    public static String sendRequest(String url, List params) {
        try {
            HttpPost httpRequst = new HttpPost(url);
            httpRequst.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            CloseableHttpClient httpclient = HttpClients.createDefault();
            CloseableHttpResponse httpResponse = httpclient.execute(httpRequst);
            System.out.println(httpResponse.getStatusLine().getStatusCode());
            HttpEntity httpEntity = httpResponse.getEntity();
            return EntityUtils.toString(httpEntity);// 取出应答字符串
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
